<?php
	namespace App\Model;
	class UserModel extends BaseModel{
		public function __construct(){
	    $this->_table="user";
	    parent::__construct();
	}
	public function queryAll($sql) {
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $sth ->fetchAll();
	}
	public function selectRolr($id) {
		$sql = sprintf("select NickName,Email,Phone from `%s` where `RolrID` = '%s'", $this -> _table, $id);
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $sth -> fetchAll();
	}
//	public function selectPower1() {
//		$sql = sprintf("select NickName,Email,Phone from `%s` where `RolrID` = 1", $this -> _table);
//		$sth = $this -> _dbHandle -> prepare($sql);
//		$sth -> execute();
//		return $sth -> fetchAll();
//	}
	public function SelectUserList($test){
		$sql = sprintf("select * from `%s` %s ORDER BY Time desc", $test, $this -> filter);
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $sth -> fetchAll();
	}
	public function Useradd($test,$data) {
		$sql = sprintf("insert into `%s` %s", $test, $this -> formatInsert($data));
		return $this -> query($sql);
	}
	public function Userupdate($data,$email) {
		$sql = sprintf("update `%s` set %s where `Email` = '%s'",$this -> _table,$this -> formatUpdate($data), $email);
		return $this -> query($sql);
	}
	public function Lupdate($test,$data,$email) {
		$sql = sprintf("update `%s` set %s where `Email` = '%s'", $test, $this -> formatUpdate($data), $email);
		return $this -> query($sql);
	}
	public function Userselect($name,$email) {
		if($name!=""){
			$sql = sprintf("select * from `%s` where `NickName` = '%s'", $this -> _table,$name);
			$sth = $this -> _dbHandle -> prepare($sql);
			$sth -> execute();
			return $sth -> fetchAll();
		}elseif($email!=""){
			$sql = sprintf("select * from `%s` where `Email` = '%s'", $this -> _table,$email);
			$sth = $this -> _dbHandle -> prepare($sql);
			$sth -> execute();
			return $sth -> fetchAll();
		}
	}
	public function Rolrselect($name,$email) {
		if($name!=""){
			$sql = sprintf("select * from `%s` where `NickName` = '%s'", $this -> _table, $name);
			$sth = $this -> _dbHandle -> prepare($sql);
			$sth -> execute();
			return $sth -> fetchAll();
		}elseif($email!=""){
			$sql = sprintf("select * from `%s` where `Email` = '%s'", $this -> _table, $email);
			$sth = $this -> _dbHandle -> prepare($sql);
			$sth -> execute();
			return $sth -> fetchAll();
		}
	}
	public function selectName($email) {
		$sql = sprintf("select id,username,nickname,email,phone,sex,address,rolrid from `%s` where `Email` = '%s'", $this -> _table, $email);
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $sth -> fetch();
	}
	public function selectItem($email) {
		$sql = sprintf("select * from `%s` where `Email` = '%s'", $this -> _table, $email);
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $sth -> fetch();
	}
	public function getMenu($sql) {
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $sth -> fetchAll();
	}
	public function addrolr($test,$data) {
		$sql = sprintf("insert into `%s` %s", $test, $this -> formatInsert($data));
		return $this -> query($sql);
	}
	public function addpower($test,$data) {
		$sql = sprintf("insert into `%s` %s", $test, $this -> formatInsert($data));
		return $this -> query($sql);
	}
}
